import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class encodingFilter implements Filter {
    private String encoding="UTF-8";
    public void destroy() {
        System.out.println("encodingFilter销毁了");
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest re=(HttpServletRequest) req;
        HttpServletResponse rp=(HttpServletResponse)resp;
        re.setCharacterEncoding(encoding);
        rp.setContentType("text/html;charset="+encoding);
        chain.doFilter(req, resp);
        System.out.println();
    }

    public void init(FilterConfig config) throws ServletException {
        //初始化web.xml参数
        encoding=config.getInitParameter("ecd");
        System.out.println("encodingFilter初始化");

    }

}
